#include "BTree.h"

BTree::BTree(int pSize, BufferManager * manager)
{
	pageSize = pSize;
	root = NULL;
	bm = manager;
	queryTimes = insertTimes = deleteTimes = 0;
}

BTree::~BTree()
{

}

bool BTree::InsertKey(KEY key)
{
	if(!root)
	{
		root = new BTNode(pageSize, true, bm, 0);
	}
	BTNode * targetNode = root->FindNode(key);
	BTNode * temp = targetNode->InsertKeyIntoLeaf(key);
	if(temp)
	{
		root = temp;
		cout << "Node " << root->GetID() << " is new root!" << endl;
	}

	insertTimes++;
	return true;
}

KEY BTree::PointQuery(KEY querykey)
{
	if(!root)
	{
		cout << "Error! build the tree first!" << endl;
	}

	queryTimes++;
	return root->PointQuery(querykey);
}
